import datetime

from django.core.mail import send_mail
from neuproject import settings
from users.models import EmailVerifyRecord


def make_confirm_string(email):
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    code = hash(email + now)
    EmailVerifyRecord.objects.create(code=code, email=email, )
    return code


def send_email_code(email, send_type='register'):
    code = make_confirm_string(email)
    email_record = EmailVerifyRecord()
    email_record.email = email
    email_record.code = code
    email_record.send_type = send_type
    email_record.send_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    email_record.save()

    # 初始化为空
    email_title = ""
    email_body = ""

    if send_type == "register":
        email_title = "东软人才湾 注册激活链接"
        email_body = "请点击下面的链接激活你的账号:http://{0}/users/active/{1}".format(settings.MY_IP, code)



    if send_type == "forget":
        email_title = "东软人才湾 密码重置"
        email_body = "请点击下面的链接重置你的密码:http://{0}/users/reset/{1}".format(settings.MY_IP, code)

    send_mail(email_title, email_body, "hehecat@foxmail.com", [email], fail_silently=False)
